<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Admin extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library('pagination');
		
	}
	function index() {
		$this->load->view ( 'admin/admin' );
	}
	function top() {
		$this->load->view ( 'admin/top' );
	}
	function sidebar() {
		$this->load->view ( 'admin/sidebar' );
	}
	function main() {
		$this->load->view ( 'admin/main' );
	}
	function manage() {
		$this->load->model ( 'Madmin' );
		$data['get_count_manage'] = $this->Madmin->get_count_manage();
		/**下面的config是实现分页的时候的一些配置*/
		$config['base_url'] = base_url().'/index.php/admin/manage/manage_list/';
		$config['total_rows'] = $data['get_count_manage'][0]->count;
		$config['per_page'] = '6';
		$config['num_links'] = 2;
		//一般来说是第三段，但是后台我们使用的第四段，估计前台页面使用的是第三段
		$config['uri_segment'] = 4;
		$config['first_link'] = 'First';
		$this->pagination->initialize($config);
		$data ['manage'] = $this->uri->segment (3);
		//$this->uri->segment(4,1)后面的1非常重要，表示如果第四段没有值，默认是1，省去了很多if判断语句。
		$data['all_manage'] = $this->Madmin->get_manage (($this->uri->segment(4,1)-1)*$config['per_page'],$config['per_page']);
		if($data['manage'] == 'manage_list'){
			$data['title'] = '管理员列表';
		}elseif ($data['manage'] == 'manage_add'){
			$data['title'] = '新增管理员';
		}elseif($data['manage'] == 'manage_update'){
			$data['title'] = '修改管理员';
		}else{
			$data['title'] = '修改管理员';
		}
		
		$data['id'] = $this->uri->segment (4);
		if($data['id']){
			$data ['get_one_manage'] = $this->Madmin->get_one_manage ($data['id']);
		}
		$this->load->view ( 'admin/manage', $data );
	}
	
	function add_manage() {
		$this->load->model ( 'Madmin' );
		$submit = $this->input->post ( 'submit' );
		if (isset ( $submit )) {
			$rows = $this->Madmin->add_manage ();
			if ($rows == 1) {
				redirect ( 'admin/manage/manage_list' );
			} else {
				//以后做一个弹出页面，或是警告返回，或是重新定向
				redirect('admin/manage/manage_add');
			}
		}
	}
	
	function update_manage() {
			$this->load->model('Madmin');
			$submit = $this->input->post('submit');
			if(isset($submit)){
				$rows = $this->Madmin->update_manage();
				if($rows == 1){
					redirect('admin/manage/manage_list');
				}else{
					//可以做一个弹出页面
					redirect('admin/manage/manage_update');
				}
			}
	}
	
	//删除管理员
	function delete_manage(){
		$this->load->model('Madmin');
		$rows = $this->Madmin->delete_manage($this->uri->segment(3));
		if($rows == 1){
			redirect('admin/manage/manage_list');
		}else{
			//以后做弹出框 返回
			redirect('admin/manage/manage_list');
		}
	}

	function nav() {
		$this->load->model('Madmin');
		$data['all_nav'] = $this->Madmin->get_nav();
		$data ['nav'] = $this->uri->segment ( 3 );
		$data['id'] = $this->uri->segment (4);
		if($data['id']){
			$data['get_one_nav'] = $this->Madmin->get_one_nav($data['id']);
			$data['all_child_nav'] = $this->Madmin->get_child_nav($data['id'] );
		}
		$this->load->view ( 'admin/nav', $data );
	}
	
	//增加导航功能实现
	function add_nav(){
		$this->load->model ( 'Madmin' );
		$submit = $this->input->post ( 'submit' );
		if (isset ( $submit )) {
			$rows = $this->Madmin->add_nav ();
			if ($rows == 1) {
				redirect('admin/nav/nav_list');
			} else {
				//以后做一个弹出页面，或是警告返回，或是重新定向
				redirect('admin/nav/nav_add');
			}
		}
	}
	
	//增加子类
	function add_nav_child(){
		$this->load->model ( 'Madmin' );
		$submit = $this->input->post ( 'submit' );
		$id = $this->uri->segment(3);
		if (isset ( $submit )) {
			$rows = $this->Madmin->add_nav_child ();
			if ($rows == 1) {
				redirect('admin/nav/nav_list');
			} else {
				//以后做一个弹出页面，或是警告返回，或是重新定向
				redirect('admin/nav/nav_child_add/'.$id);
			}
		}
		
	}
	//修改导航功能的实现
	function update_nav() {
		$this->load->model('Madmin');
		$submit = $this->input->post('submit');
		$id = $this->uri->segment (3);
		if(isset($submit)){
			$rows = $this->Madmin->update_nav();
			if($rows == 1){
				redirect('admin/nav/nav_list');
			}else{	
				echo '<script type="text/javascript">alert("您没有进行任何修改，您再试一次");history.back();</script>';
				//实验了一下，用js跳转比较慢，但是用redirect函数手册没有找到弹出框的参数，蛋疼
				//redirect('admin/nav/nav_update/'.$id,'location');
			}
		}
	}
	
	//update child
	function update_nav_child(){
		$this->load->model('Madmin');
		$submit = $this->input->post('submit');
		if(isset($submit)){
			$rows = $this->Madmin->update_nav_child();
			if($rows == 1){
				redirect('admin/nav/nav_list');
			}else{
				echo '<script type="text/javascript">alert("您没有进行任何修改，您再试一次");history.back();</script>';
				//实验了一下，用js跳转比较慢，但是用redirect函数手册没有找到弹出框的参数，蛋疼
				//redirect('admin/nav/nav_update/'.$id,'location');
			}
		}
	}
	
	//删除导航功能实现
	function delete_nav(){
		$this->load->model('Madmin');
		$rows = $this->Madmin->delete_nav($this->uri->segment(3));
		if($rows == 1){
			redirect('admin/nav/nav_list');
		}else{
			//以后做弹出框 返回
			redirect('admin/nav/nav_list');
		}
		
	}
	
	//删除子类导航
	function delete_child_nav(){
		$this->load->model('Madmin');
		$rows = $this->Madmin->delete_child_nav($this->uri->segment(3));
		$id  = $this->uri->segment(4);
		if($rows == 1){
			redirect('admin/nav/nav_child_list/'.$id);
		}else{
			//以后做弹出框 返回
			redirect('admin/nav/nav_child_list/'.$id);
		}
		
	}
	
	function premission() {
		$data ['premission'] = $this->uri->segment ( 3 );
		$this->load->view ( 'admin/premission', $data );
	}
	function level() {
		$data ['level'] = $this->uri->segment ( 3 );
		$this->load->view ( 'admin/level', $data );
	}
	function sidebarn() {
		$this->load->view ( 'admin/sidebarn' );
	}
	
	function content() {
		//配置ckfinder和ckeditor
		//加载类库ckeditor
		$this->load->library('ckeditor');
		//$this->ckeditor = new CKEditor();
		//配置路径
		$this->ckeditor->basePath = base_url() . 'ckeditor/';
		$this->ckeditor->returnOutput = true;
		//加载类库ckfinder
		$this->load->library('ckfinder');
		//$this->ckfinder->basePath = site_url() . 'ckfinder/';
		//$this->ckfinder->SetupCKEditorObject($this->ckeditor);
		//将ckfinder和ckdeitor关联起来因为在耳机目录所以路径是/ci/ckfinder/
		CKFinder::SetupCKEditor($this->ckeditor,'/bufan-studio/master/ckfinder/');
		//下边这句就是生成的编辑器了，在视图页面echo $ck;就可以啦
		$data['ck'] = $this->ckeditor->editor('text','请输入正文');
		$this->load->model('Madmin');
		$data['all_nav'] = $this->Madmin->get_all_parent_nav();
		$data['html'] = array();
		if(!empty($data['all_nav'])){
			foreach($data['all_nav'] as $rows){
				$data['html'].= '<optgroup label="'.$rows->nav_name.'">'."\r\n";
				$id = $rows->id;
				$data['all_child_nav'] = $this->Madmin->get_all_child_nav($id);
				if($data['all_child_nav']){
					foreach ($data['all_child_nav'] as $rows){
						$data['html'] .= '<option value="'.$rows->id.'">'.$rows->nav_name.'</option>'."\r\n";
					}
				}
				
				$data['html'].='</optgroup>';
			}
		}
		//$id = $data['all_nav']->id;
		//$data['all_child_nav'] = $this->Madmin->get_all_child_nav($id);
		$data['all_content'] = $this->Madmin->get_content();
		$data ['content'] = $this->uri->segment ( 3 );
	
		$this->load->view ( 'admin/content', $data );
	}
	
	function add_content(){
		$this->load->model ( 'Madmin' );
		$submit = $this->input->post ( 'submit' );
		if (isset ( $submit )) {
			$rows = $this->Madmin->add_content ();
			if ($rows == 1) {
				redirect('admin/content/content_list');
			} else {
				//以后做一个弹出页面，或是警告返回，或是重新定向
				redirect('admin/content/content_add');
			}
		}
	}
		
	function comment() {
		$this->load->view ( 'admin/comment' );
	}
	function rotatain() {
		$data ['rotatain'] = $this->uri->segment ( 3 );
		$this->load->view ( 'admin/rotatain', $data );
	}
	function vote() {
		$data ['vote'] = $this->uri->segment ( 3 );
		$this->load->view ( 'admin/vote', $data );
	}
	function sidebaru() {
		$this->load->view ( 'admin/sidebaru' );
	}
	function user() {
		$data ['user'] = $this->uri->segment ( 3 );
		$this->load->view ( 'admin/user', $data );
	}
	function sidebars() {
		$this->load->view ( 'admin/sidebars' );
	}
	function system() {
		$this->load->view ( 'admin/system' );
	}

}